package org.kinglone.back.music.action;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.kinglone.back.music.model.Music;
import org.kinglone.back.music.service.MusicService;
import org.kinglone.common.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;


/**
 * 音乐控制层
 * @author yejinlong
 * 2017年8月2日
 * @version 1.0
 */
@Controller
@RequestMapping("/music")
public class MusicAction {

	@Resource(name="musicService")
	private MusicService musicService;

	/**
	 * 跳转到音乐管理页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/musicPage.jspx")
	public ModelAndView musicPage(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/WEB-INF/jsp/back/music/musicPage.jsp");
		return mv;
	}

	/**
	 * 跳转到添加音乐页面
	 * @param request
	 * @return
	 */
	@RequestMapping("/showAddMusic.jspx")
	public ModelAndView showAddMusic(HttpServletRequest request){
		ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/back/music/addMusic.jsp");
		return modelAndView;
	}

	/**
	 * 分页查询音乐列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/selMusicList.jspx")
	public  JSONMessageView selMusicList(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1,"查询失败!",null);
		try{
			String pageNo = request.getParameter("pageNo");
			String pageSize = request.getParameter("pageSize");
			PageQuery pageQuery = new PageQuery();
			pageQuery.setStart((Integer.parseInt(pageNo)-1)*Integer.parseInt(pageSize));
			pageQuery.setLength(Integer.parseInt(pageSize));
			PageResults<Music> list = musicService.findMusicByPage(pageQuery);
			JSONArray totalJsonArray = new JSONArray();
			JSONObject totalJsonObject = new JSONObject();
			JSONArray jsonArray=new JSONArray();
			for(Music music : list.getResults()){
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("sysid",music.getSysid());
				jsonObject.put("musicName",music.getMusicName());
				jsonObject.put("createtime",music.getCreateTime());
				jsonObject.put("musicUrl",music.getMusicUrl());
				jsonObject.put("sequence",music.getSequence());
				jsonObject.put("enabled",music.getEnabled());
				jsonArray.add(jsonObject);
			}
			totalJsonObject.put("pagetotal", list.getPageCount());//总页数
			totalJsonObject.put("info", jsonArray);
			totalJsonArray.add(totalJsonObject);
			json.setCode(0);
			json.setMessage("查询成功!");
			json.setContent(totalJsonArray);
			return json;

		}catch (Exception e){
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("系统错误!");
			return json;
		}
	}

	/**
	 * 上传音乐
	 * @param request
	 * @return
	 */
	@RequestMapping("/musicUploadTemp.jspx")
	public JSONMessageView addMusic(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1, "上传失败!", null);
		try {
			Map<String, String> map = FileUtils.saveMusicByRequest(request, "blog");
			for (String key : map.keySet()) {
				json.setCode(0);
				json.setMessage("上传成功!");
				json.setContent(map.get(key));
				System.out.println(json);
			}
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("服务器异常,请稍候再试!");
			return json;
		}
	}

	/**
	 * 保存音乐
	 * @param request
	 * @return
	 */
	@RequestMapping("/submitAddMusic.jspx")
	public JSONMessageView submitAddMusic(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1,"保存失败!",null);
		try{
			String musicName = request.getParameter("musicName");
			String music_url = request.getParameter("musicUrl");
			String sequence = request.getParameter("sequence");
			if(StringUtil.isNullOrEmpty(musicName)){
				json.setCode(-1);
				json.setMessage("请输入音乐标题!");
				return json;
			}
			if(StringUtil.isNullOrEmpty(sequence)){
				json.setCode(-1);
				json.setMessage("请输入序号!");
				return json;
			}
			if(StringUtil.isNullOrEmpty(music_url)){
				json.setCode(-1);
				json.setMessage("请输入音乐链接!");
				return json;
			}
			Music music = new Music();
			music.setCreateTime(DateUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
			music.setMusicName(musicName);
			music.setMusicUrl(music_url);
			music.setSequence(sequence);
			music.setEnabled("Y");
			musicService.save(music);
			json.setCode(0);
			json.setMessage("保存成功!");
			return json;
		}catch (Exception e){
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("系统错误!");
			return json;
		}
	}

	/**
	 * 删除音乐
	 * @param request
	 * @return
	 */
	@RequestMapping("/delMusic.jspx")
	public JSONMessageView delMusic(HttpServletRequest request){
		JSONMessageView json = new JSONMessageView(-1,"删除失败!",null);
		try {
			String musicId = request.getParameter("musicId");
			if(StringUtil.isNullOrEmpty(musicId)){
				json.setCode(-1);
				json.setMessage("请选择需要删除的项!");
				return json;
			}
			musicService.deleteById(musicId);
			json.setCode(0);
			json.setMessage("删除成功!");
			return json;
		}catch (Exception e){
			e.printStackTrace();
			json.setCode(-1);
			json.setMessage("系统错误!");
			return json;
		}
	}

}
